﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniLua;

public class CardsMgr : MonoBehaviour {

    public Card Card;
    public Transform InHand;
    public GameObject DrawPile;
    public GameObject DiscardPile;

    public Transform InvisibleCardsRoot;

    // key 是 hLuaTable
    Dictionary<string, List<Card>> cards = new Dictionary<string, List<Card>>();

    UIMgr uiMgr;
    void Awake()
    {
        CombatMgr.cMgr = this;

        cards["draw"] = new List<Card>();
        cards["inhand"] = new List<Card>();
        cards["discard"] = new List<Card>();
        cards["exhuasted"] = new List<Card>();

        uiMgr = GameObject.Find("UIRoot").GetComponent<UIMgr>();
    }

    // 移除所有卡牌
    public void Clear()
    {
        foreach (var lst in cards.Values)
        {
            foreach (var c in lst)
                Destroy(c.gameObject);

            lst.Clear();
        }
    }

    Card FindCard(LuaCard card, out string pile)
    {
        foreach (var p in cards.Keys)
        {
            var lst = cards[p];
            foreach (var c in lst)
            {
                if (c.hCard == card)
                {
                    pile = p;
                    return c;
                }
            }
        }

        pile = null;
        return null;
    }

    // 增加卡牌到指定位置
    public void AddCard(LuaCard card, string pile)
    {
        var c = Instantiate(Card) as Card;
        c.gameObject.SetActive(true);
        c.hCard = card;
        cards[pile].Add(c);
        c.transform.SetParent(InvisibleCardsRoot);

        DrawPile.SetActive(cards["draw"].Count > 0);
        DiscardPile.SetActive(cards["discard"].Count > 0);

        c.SetCardInfo(card);

        c.OnDragging += OnCardDragging;
        c.OnDragEnded += OnDragEnded;
    }

    private void OnCardDragging(LuaCard card, Vector2 from, Vector2 to)
    {
        var tar = uiMgr.CheckTarget(to);
        uiMgr.ShowArrow(true, from, to, card.CanUse(tar == null ? null : tar.hMonster));
    }

    public static CombatMgr cbtMgr = null;
    private void OnDragEnded(LuaCard card, Vector2 sp)
    {
        uiMgr.ShowArrow(false, Vector2.zero, Vector2.zero, false);
        var tar = uiMgr.CheckTarget(sp);
        if (card.CanUse(tar == null ? null : tar.hMonster))
            card.Use(tar == null ? null : tar.hMonster);
    }

    // 移动卡牌
    public void MoveCard(LuaCard card, string fromPile, string toPile)
    {
        var pile = "";

        var c = FindCard(card, out pile);
        cards[pile].Remove(c);
        cards[toPile].Add(c);

        if (fromPile == "inhand" && toPile != "inhand")
            c.transform.SetParent(InvisibleCardsRoot);
        else if (fromPile != "inhand" && toPile == "inhand")
            c.transform.SetParent(InHand);

        c.transform.localScale = Vector3.one;
        c.transform.localPosition = Vector3.zero;

        DrawPile.SetActive(cards["draw"].Count > 0);
        DiscardPile.SetActive(cards["discard"].Count > 0);
    }
}
